/* 
 * File:   Grid.h
 * Author: eliana
 *
 * Created on October 20, 2012, 7:46 PM
 */

#ifndef GRID_H
#define	GRID_H

#include <vector>
#include "Ray.h"
#include "Object.h"
#include "GridCell.h"
#include "Box.h"

class GridIterator;

class Grid 
{
public:
    
    /**
     * Constructor
     * @param objects Vector of objects that will be distributed in the grid
     */
    Grid( std::vector<Object*>& objects );

    /**
     * Destructor
     */
    virtual ~Grid();
    
    /**
     * Creates an iterator to "walk" through the grid
     * @param ray Ray that will guide the iteration
     * @return Pointer to the built iterator
     */
    virtual GridIterator* getIterator( Ray& ray ) = 0;
            
    /**
     * @return Axis aligned bounding box containing all the objects in the grid
     */
    Box* getBoundingBox();    
    
protected:
    
    /**
     * Builds the bounding box of the grid from the objects of the scene
     * @param objects
     */
    void buildBoundingBox( std::vector<Object*>& objects );
    
    /**
     * Says if two axis aligned boxes overlap
     * @param b1Min
     * @param b1Max
     * @param b2Min
     * @param b2Max
     * @return true if overlap, false otherwise
     */
    bool boxOverlap( Vector4D aMin, Vector4D aMax, Vector4D bMin, Vector4D bMax );
    
    /** Grid cells */
    std::vector<GridCell*> _cells;
    
    /** Axis aligned bounding box containing all the objects of the scene */
    Box* _boundingBox;
};

#endif	/* GRID_H */

